//
//  GameDetailImagesCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/5.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class GameDetailImagesCell: BaseTableViewCell {

    @IBOutlet weak var bgView: UIView!
    
    @IBOutlet weak var bottomMargin: NSLayoutConstraint!
    
    
    lazy var collectionView : UICollectionView = {
        
        let collet = UICollectionView(frame:CGRect(x: 0, y: 0, width: KScreenW, height: KScreenW / 5 * 3), collectionViewLayout: GameDetailFlowLayout())
        
        collet.dataSource = self
        
        collet.delegate = self
        
        
        collet.backgroundColor = UIColor.white
        
        collet.register(UINib(nibName: self.StringFromClass(classType: GameCollectionCell.self), bundle: nil), forCellWithReuseIdentifier: GameCollectionCell.identifer())
        
        return collet
    }()
    
    
    
    var model : GameDetailModel?{
        
        didSet{
            
            if model == nil {
                return
            }
            
            
            collectionView.reloadData()
            
            
        }
        
        
    }


    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        bottomMargin.constant = -8
        
        bgView.addSubview(collectionView)

        // Initialization code
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
      
//
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}


extension GameDetailImagesCell : UICollectionViewDelegate,UICollectionViewDataSource{


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        if model == nil {
            return 0

        } else {
            return 1

        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return model?.imgs.count ?? 0
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GameCollectionCell.identifer(), for: indexPath) as! GameCollectionCell
        
        
        cell.urlStr = model!.imgs[indexPath.item]
        
        
        return cell
        
    }





}


class GameDetailFlowLayout: UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        
        itemSize = CGSize(width: KScreenW - 20, height: KScreenW / 5 * 3)
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = .horizontal
        sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        
}
    
}
